/*
 **********************************************************************************
 * Copyright www.ebidding.com.cn 2017年12月07日 Authors: 符强年 <fqn@ebidding.com.cn>*
 **********************************************************************************
 */
package com.example.demo.common.msg;

import com.example.demo.common.msg.disruptor.MessageEvent;
import com.example.demo.common.msg.disruptor.MessageEventPublisher;
import com.example.demo.common.msg.disruptor.MessageEventVo;
import com.example.demo.common.msg.vo.MessageType;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.dsl.Disruptor;

/**
 * 作者: 符强年
 * 创建时间: 2017年12月07日
 * 功能描述:
 * 版本: 1.0.0
 */

public class DisruptorMessageSender implements MessageSender,EventHandler<MessageEvent> {


    private MessageEventPublisher eventPublisher;


    private Disruptor disruptor;


    MessageEntitySendHandlerFactory handlerFactory;

    @Override
    public void send(MessageType type, MessageEventVo vo) {

            eventPublisher.publish(type,vo);
    }


    @Override
    public void onEvent(MessageEvent event, long l, boolean b) throws Exception {

        MessageType messageType = event.getMessageType();
        MessageEventVo vo = event.getMessageEventVo();
        MessageEntitySendHandler handler = handlerFactory.getHandler(messageType);
        handler.send(vo.getMsgRecord(),vo.getReceiver(),vo.getMessageWrapper());

        event.setMessageEventVo(null);
        event.setMessageType(null);

    }


    public void setEventPublisher(MessageEventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }

    public Disruptor getDisruptor() {
        return disruptor;
    }

    public void setDisruptor(Disruptor disruptor) {
        this.disruptor = disruptor;
    }


    public void setHandlerFactory(MessageEntitySendHandlerFactory handlerFactory) {
        this.handlerFactory = handlerFactory;
    }
}
